package com.hanamaki.dataStructDemo;

import com.hanamaki.oop.demo10.A;

import java.util.*;

public class ArrayListTest {
    public static void main(String[] args) {

        //Object ：代表我集合当中可以存什么数据类型的 值
        List<String> list = new ArrayList<>();
        // 向集合末尾添加值
        list.add("test");
        list.add("cest");
        list.add("test");
        //  向集合中的某个位置添加值
        list.add(0, "first");

//       判断集合中是否有当前值
        System.out.println(list.contains("first"));

        System.out.println(list);

        System.out.println(list.get(2));

        // 获取test值首次出现的位置
        System.out.println(list.indexOf("test"));

        // 判断list集合是否为空
        System.out.println(list.isEmpty());

        list.set(1, "second");

        System.out.println(list);

        // 获得集合的大小
        System.out.println(list.size());

        // 集合排序
        Collections.sort(list);

        // 将集合转换成数组
        System.out.println(list);
        Object[] objects = list.toArray();
        System.out.println(objects);
        System.out.println(Arrays.toString(objects));

        System.out.println("数组遍历");
        for (int i = 0; i < objects.length; i++) {
            System.out.print(objects[i] + "\t");
        }

        for (Object object : objects) {
            System.out.print(object + "\t");
        }

        System.out.println();

        System.out.println("集合遍历");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + "\t");
        }
        System.out.println("\n" + "增强for循环");

        for (String s : list) {
            System.out.print(s + "\t");
        }
        System.out.println("\n使用迭代器遍历集合");
        Iterator<String> iterator = list.iterator();  // alt +enter
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);
        }
    }
}
